package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.api.User.IUserClient;
import com.heima.article.service.BehaviorService;
import com.heima.common.cache.CacheService;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

import static com.heima.article.interceptor.TokenInterceptor.THREADLOCAL;

@Service
public class BehaviorServiceImpl implements BehaviorService {
    @Autowired
    CacheService cacheService;
    @Autowired
    IUserClient userClient;


    @Override
    public ResponseResult CollectionBehavior(CollectionBehaviorDto collectionBehaviorDto) {
        //文章id
        Long entryId = collectionBehaviorDto.getEntryId();
        //操作 0收藏    1取消收藏
        short operation = collectionBehaviorDto.getOperation();
        //发布时间
        Date publishedTime = collectionBehaviorDto.getPublishedTime();
        //type类型
        short type = collectionBehaviorDto.getType();
        //用户id
        String userId = THREADLOCAL.get();

        switch (type){
            case (short)1:
                cacheService.hDelete(String.valueOf(entryId),"CollcetBehaviors"+userId );
                break;
            case (short)0:
                //判断是否已经存在相应的key,不存在则创建并设置初始值为1
                if(!cacheService.hExists(String.valueOf(entryId), "CollcetBehaviors"+userId)){
                    cacheService.hPut(String.valueOf(entryId),"CollcetBehaviors"+userId,"1");
                    return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
                }else {
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已经收藏");
                }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 数据回显行为
     * @param articleInfoDto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto articleInfoDto) {
        //文章id
        Long articleId = articleInfoDto.getArticleId();
        //作者id
        Long authorId = articleInfoDto.getAuthorId();
        //用户id
        String userId = THREADLOCAL.get();

        HashMap<String, Boolean> ResultMap = new HashMap<>();
        boolean test = cacheService.hExists(String.valueOf(articleId), "CollcetBehaviors" + userId);
        //判断是否收藏
        if(cacheService.hExists(String.valueOf(articleId),"CollcetBehaviors"+userId)){
            ResultMap.put("iscollection",true);
        }else {
            ResultMap.put("iscollection",false);
        }

        //判断是否喜欢
        if(cacheService.hExists(String.valueOf(articleId), "likeBehavior"+userId)){
            ResultMap.put("islike",true);
        }else {
            ResultMap.put("islike",false);
        }

        //判断是否不喜欢
        if(cacheService.hExists(String.valueOf(articleId), "unLikeBehavior"+userId)){
            ResultMap.put("isunlike",true);
        }else {
            ResultMap.put("isunlike",false);
        }

        articleInfoDto.setUserId(Long.parseLong(userId));
        //判断是否是粉丝
        ResponseResult responseResult = userClient.RelationSearch(articleInfoDto);
        if(responseResult.getCode()==200){
            ResultMap.put("isfollow",true);
        }else {
            ResultMap.put("isfollow",false);
        }
        return ResponseResult.okResult(ResultMap);
    }
}
